package com.smsp.controller;

import com.github.pagehelper.PageInfo;
import com.mysql.fabric.Server;
import com.smsp.common.ServerResponse;
import com.smsp.entity.MessageDetail;
import com.smsp.entity.User;
import com.smsp.service.MessageDetailService;
import com.smsp.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.smsp.service.UserService;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DWJ
 */
@Controller
@RequestMapping("msgdetail/")
public class MessageDetailController {
    @Autowired
    MessageDetailService messageDetailService;

    public Logger logger = LoggerFactory.getLogger(MessageDetailController.class);

    /**
     * 短信明细列表视图
     *
     * @return
     */
    @RequestMapping(value = "messageDetailPage.do")
    public String messageDetailPage() {
        return "messageDetail";
    }

    /**
     * 我的明细列表视图
     */
    @RequestMapping(value = "myMessageDetailPage.do")
    public String myMessageDetailPage() {
        return "myMessageDetail";
    }

    @RequestMapping(value = "sendMessagePage.do")
    public String sendMessagePage() {
        return "sendMessagePage";
    }

    @RequestMapping(value = "delete.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse delete(MessageDetail messageDetail) {
        ServerResponse serverResponse = messageDetailService.deleteMessage(messageDetail.getId());
        return serverResponse;
    }

    @RequestMapping(value = "selectAll.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse selectAll() {
        ServerResponse serverResponse = messageDetailService.selectAll();
        return serverResponse;
    }


    @RequestMapping(value = "selectListByUserId.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse selectListByUserId(HttpSession session) {
        ServerResponse serverResponse = messageDetailService.selectListByUserId((User) session.getAttribute("user"));
        return serverResponse;
    }

    @RequestMapping(value = "create.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse create(@Validated @RequestBody MessageDetail messageDetail, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            logger.debug(allErrors.get(0).getDefaultMessage(), allErrors);
            return ServerResponse.createByErrorMessage(allErrors.get(0).getDefaultMessage());
        }

        return messageDetailService.createMessage(messageDetail);
    }

    @RequestMapping(value = "listPage.do", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> findAllByPage(int page, int pageSize) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PageInfo<MessageDetail> pager = messageDetailService.findMessageDetailByPage(page, pageSize);
        //总条数
        resultMap.put("total", pager.getTotal());
        //获取每页数据
        resultMap.put("rows", pager.getList());
        return resultMap;

    }
}
